package com.lemon.smartsafetynew.ui.adapter.work;

import android.graphics.Typeface;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.work.OverViewVo;
import com.lemon.smartsafetynew.ui.widget.CountNumberView;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;
import com.tmall.ultraviewpager.UltraViewPager;

import java.util.List;

import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/10/19.
 */

public class WorkBannerViewRec implements AdapterItem<MainRcvItemBase<OverViewVo>> {

    private UltraViewPager ultraViewPager;

    @Override
    public int getLayoutResId() {
        return R.layout.view_banner;
    }

    @Override
    public void bindViews(View root) {
        ultraViewPager = (UltraViewPager) root.findViewById(R.id.banner);
    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(MainRcvItemBase<OverViewVo> itemBase, int position) {

        if (RcvViewType.MainRcvViewType.TYPE_BANNER != itemBase.getViewType()) {
            return;
        }

        ultraViewPager.setScrollMode(UltraViewPager.ScrollMode.HORIZONTAL);
        PagerAdapter adapter = new UltraPagerAdapter(itemBase.getData());
        ultraViewPager.setAdapter(adapter);

    }

    public class UltraPagerAdapter extends PagerAdapter {

        private OverViewVo overViewVo;

        public UltraPagerAdapter(OverViewVo overViewVo) {
            this.overViewVo = overViewVo;
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            Typeface typeFace = Typeface.createFromAsset(IApplication.context().getAssets(),"fonts/impact.ttf");

            LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(container.getContext()).inflate(R.layout.item_banner_layout, null);

            TextView welcome = (TextView) linearLayout.findViewById(R.id.welcome_info_tv);
            TextView date = (TextView) linearLayout.findViewById(R.id.date_tv);
            CountNumberView leftOne = (CountNumberView) linearLayout.findViewById(R.id.left_one);
            TextView leftOneTv = (TextView) linearLayout.findViewById(R.id.left_one_name_tv);
            CountNumberView leftTwo = (CountNumberView) linearLayout.findViewById(R.id.left_two);
            TextView leftTwoTv = (TextView) linearLayout.findViewById(R.id.left_two_name_tv);
            CountNumberView leftThree = (CountNumberView) linearLayout.findViewById(R.id.left_three);
            TextView leftThreeTv = (TextView) linearLayout.findViewById(R.id.left_three_name_tv);
            CountNumberView rightOne = (CountNumberView) linearLayout.findViewById(R.id.right_one);
            TextView rightOneTv = (TextView) linearLayout.findViewById(R.id.right_one_name_tv);
            CountNumberView rightTwo = (CountNumberView) linearLayout.findViewById(R.id.right_two);
            TextView rightTwoTv = (TextView) linearLayout.findViewById(R.id.right_two_name_tv);
            CountNumberView rightThree = (CountNumberView) linearLayout.findViewById(R.id.right_three);
            TextView rightThreeTv = (TextView) linearLayout.findViewById(R.id.right_three_name_tv);

            leftOneTv.setTypeface(typeFace);
            leftTwoTv.setTypeface(typeFace);
            leftThreeTv.setTypeface(typeFace);
            rightOneTv.setTypeface(typeFace);
            rightTwoTv.setTypeface(typeFace);
            rightThreeTv.setTypeface(typeFace);

            leftOne.setTypeface(typeFace);
            leftTwo.setTypeface(typeFace);
            leftThree.setTypeface(typeFace);
            rightOne.setTypeface(typeFace);
            rightTwo.setTypeface(typeFace);
            rightThree.setTypeface(typeFace);

            //多页面样例
//            switch (position) {
//                case 0:
//                    linearLayout.setBackgroundColor(Color.parseColor("#2196F3"));
//                    break;
//            }

            date.setText("" + CommonUtil.getNowTimeStr(DateTypeEnum.MONTH));

            if (UserTypeUtil.isGovType(AccountUtil.getUserType())) { //政府端
                welcome.setText("欢迎" + CommonUtil.avoidNullString(AccountUtil.getGovName())
                        + " " + CommonUtil.avoidNullString(AccountUtil.getUserName()) + " "
                        + CommonUtil.avoidNullString(AccountUtil.getUserTypeName()) + " "
                        + "使用本系统！");
            } else {                                                 //企业端
                welcome.setText("欢迎" + CommonUtil.avoidNullString(AccountUtil.getCompanyName())
                        + " " + CommonUtil.avoidNullString(AccountUtil.getUserName()) + " "
                        + CommonUtil.avoidNullString(AccountUtil.getUserTypeName()) + " "
                        + "使用本系统！");
            }

            if (UserTypeUtil.isGovType(AccountUtil.getUserType())) { //政府端
                leftOneTv.setText(leftOneTv.getContext().getString(R.string.organization_total_number));
                leftOne.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getTotal_company_num())), CountNumberView.INTREGEX);

                leftTwoTv.setText("已部署：");
                leftTwo.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getCompany_num_with_item())), CountNumberView.INTREGEX);

                leftThreeTv.setText("未部署：");
                if (overViewVo.getTotal_company_num() != null && overViewVo.getCompany_num_with_item() != null) {
                    leftThree.showNumberWithAnimation(
                            Float.valueOf(CommonUtil.avoidNullString(overViewVo.getTotal_company_num()- overViewVo.getCompany_num_with_item())),
                            CountNumberView.INTREGEX);
                } else {
                    leftThree.showNumberWithAnimation(0, CountNumberView.INTREGEX);
                }

            } else { //企业端
                leftOneTv.setText("检查点数量：");
                leftOne.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getTotal_item_num())), CountNumberView.INTREGEX);

                leftTwoTv.setText("正常：");
                leftTwo.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getGreen_item_num())), CountNumberView.INTREGEX);

                leftThreeTv.setText("异常：");
                leftThree.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getRed_item_num())), CountNumberView.INTREGEX);
            }

            // ========== 通用 ==========

            rightOneTv.setText("上报隐患：");
            rightOne.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getTotal_danger_num())), CountNumberView.INTREGEX);

            rightTwoTv.setText("已整改：");
            rightTwo.showNumberWithAnimation(Float.valueOf(CommonUtil.avoidNullString(overViewVo.getRectify_num())), CountNumberView.INTREGEX);

            rightThreeTv.setText("未整改：");
            if (overViewVo.getTotal_danger_num() != null
                    && overViewVo.getRectify_num() != null
                    && overViewVo.getCount_closed_danger() != null && overViewVo.getOver_rectify_num() != null) {
                rightThree.showNumberWithAnimation(
                        Float.valueOf(CommonUtil.avoidNullString(overViewVo.getTotal_danger_num()
                                - overViewVo.getRectify_num() - overViewVo.getCount_closed_danger()
                                - overViewVo.getOver_rectify_num())),
                        CountNumberView.INTREGEX);
            } else {
                rightThree.showNumberWithAnimation(0, CountNumberView.INTREGEX);
            }


            container.addView(linearLayout);
            return linearLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            LinearLayout view = (LinearLayout) object;
            container.removeView(view);
        }
    }

}
